import java.time.LocalDateTime;

public class Main {
    public static void main(String[] args) {
        try {
            // 初始化用户服务
            UserService userService = new UserService();
            // 创建用户
            SystemAdmin admin = new SystemAdmin("admin1", "admin", "admin123");
            userService.addAdmin(admin);
            Manager manager = new Manager("mgr1", "manager", "mgr123");
            Clerk clerk = new Clerk("clerk1", "clerk", "clerk123");

            // 系统管理员添加店长
            userService.addManager(admin, manager);
            // 店长添加售票员
            userService.addClerk(manager, clerk);

            // 初始化影院
            Cinema cinema = new Cinema("星河影院");

            // 系统管理员添加放映厅（成功案例）
            Hall hallA = new Hall("A厅", 5, 6);
            Hall hallB = new Hall("B厅", 4, 8);
            cinema.addHall(admin, hallA);
            cinema.addHall(admin, hallB);
            System.out.println("【管理员成功添加放映厅】");
            for (Hall h : cinema.getAllHalls()) {
                System.out.println("放映厅：" + h.getName() + ", 行数：" + h.getRows() + ", 列数：" + h.getCols());
            }

            // 店长添加电影（成功案例）
            Movie movie1 = new Movie("1", "流浪地球2");
            Movie movie2 = new Movie("2", "满江红");
            cinema.addMovie(manager, movie1);
            cinema.addMovie(manager, movie2);
            System.out.println("\n【店长成功添加电影】");
            for (Movie m : cinema.getAllMovies()) {
                System.out.println("电影：" + m.getName());
            }

            // 店长添加排片（成功案例）
            Schedule s1 = new Schedule(movie1, hallA, LocalDateTime.of(2024, 6, 1, 14, 0));
            Schedule s2 = new Schedule(movie2, hallB, LocalDateTime.of(2024, 6, 1, 15, 0));
            cinema.addSchedule(manager, s1);
            cinema.addSchedule(manager, s2);
            System.out.println("\n【店长成功添加排片】");
            for (Schedule s : cinema.getSchedules()) {
                System.out.println(s.getMovie().getName() + " " + s.getHall().getName() + " " + s.getTime());
            }

            // 售票员售票（成功案例）
            System.out.println("\n【售票员售票】");
            cinema.sellTicket(clerk, s1, 1, 2, "13800000001");
            System.out.println("A厅第2行第3列已售出，手机号：" + hallA.getSeat(1, 2).getPhone());

            // 权限异常演示
            System.out.println("\n【权限异常演示】");
            try {
                cinema.addHall(manager, new Hall("C厅", 3, 3));
            } catch (PermissionException e) {
                System.out.println("店长添加放映厅异常: " + e.getMessage());
            }
            try {
                cinema.addMovie(clerk, new Movie("3", "无名"));
            } catch (PermissionException e) {
                System.out.println("售票员添加电影异常: " + e.getMessage());
            }
            try {
                cinema.sellTicket(manager, s1, 2, 2, "13800000002");
            } catch (PermissionException e) {
                System.out.println("店长售票异常: " + e.getMessage());
            }
            try {
                cinema.removeSchedule(clerk, s1);
            } catch (PermissionException e) {
                System.out.println("售票员删除排片异常: " + e.getMessage());
            }

            // 售票异常演示
            System.out.println("\n【售票异常演示】");
            try {
                cinema.sellTicket(clerk, s1, 1, 2, "13800000003");
            } catch (TicketException e) {
                System.out.println("重复售票异常: " + e.getMessage());
            }
            try {
                cinema.sellTicket(clerk, s1, 10, 0, "13800000004");
            } catch (Exception e) {
                System.out.println("越界售票异常: " + e.getMessage());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
} 